package com.utils {

	import com.bases.TTextInput;
	import com.webgame.geom.ColorMatrix;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BitmapFilterType;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GradientGlowFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.describeType;
	import flash.utils.getQualifiedClassName;

	/**
	 * 存放一些工具类
	 * @author tamt 
	 */
	public class SCUiTool {

		/**
		 * 创建窗口标题的文本框
		 */
		public static function buildWindowTitleTextField() : TextField {
			var tf : TextField;
			
			tf = new TextField();
			tf.autoSize = 'left';
			tf.textColor = 0xC0F40E;
			
			var tfm : TextFormat = new TextFormat('中易宋体', 20, 0xC0F40E);
			tf.setTextFormat(tfm);
			tf.defaultTextFormat = tfm;
			
			var distance : Number = 0;
			var angleInDegrees : Number = 0;
			var colors : Array = [0x000000, 0x000000];
			var alphas : Array = [0, 1];
			var ratios : Array = [0, 100];
			var blurX : Number = 4;
			var blurY : Number = 4;
			var strength : Number = 2;
			var quality : Number = BitmapFilterQuality.LOW;
			var type : String = BitmapFilterType.OUTER;
			var knockout : Boolean = false;
			tf.filters = [new GradientGlowFilter(distance, angleInDegrees, colors, alphas, ratios, blurX, blurY, strength, quality, type, knockout)];
			
			return tf;
		}

		/**
		 * 创建按钮标签的文本框.
		 * @param state		'up', 'over', 'down', 'click'
		 */
		public static function buildButtonLabelTextField(state : String = 'up', color : Number = 0xF9D8AD, growBorder : Boolean = true) : TextField {
			var tf : TextField;
			var states : Array = ['up', 'over', 'down', 'click'];
			//			var tColors : Array = [0xF9D8AD, 0xFA8888, 0xF9ABF4, 0xF9ABF4];			var tColors : Array = [color, color, color, color];
			var bColors : Array = [0x261602, 0x261602, 0x261602, 0x261602];
			
			var i : int = states.indexOf(state);
			if(i < 0)i = 0;
			
			tf = new TextField;
			tf.autoSize = 'center';
			//up, over, down
			tf.textColor = tColors[i];
			tf.mouseEnabled = tf.mouseWheelEnabled = tf.selectable = false;
			
			var distance : Number = 0;
			var angleInDegrees : Number = 0;
			var colors : Array = [bColors[i], bColors[i]];
			var alphas : Array = [0, 1];
			var ratios : Array = [0, 100];
			var blurX : Number = 2;
			var blurY : Number = 2;
			var strength : Number = 255;
			var quality : Number = BitmapFilterQuality.LOW;
			var type : String = BitmapFilterType.OUTER;
			var knockout : Boolean = false;
			if(growBorder)tf.filters = [new GradientGlowFilter(distance, angleInDegrees, colors, alphas, ratios, blurX, blurY, strength, quality, type, knockout)];
			
			return tf;
		}

		/**
		 * 创建一个黑白滤镜.
		 */
		public static function buildGrayFilter() : ColorMatrixFilter {
			var filter : ColorMatrixFilter;
			var cm : ColorMatrix = new ColorMatrix();
			cm.adjustColor(0, 0, -100, 0);
			filter = new ColorMatrixFilter(cm);
			return filter;
		}

		/**
		 * 创建一个静态文本框(不具有交互性)
		 */
		public static function buildStaticTextField(txt : String = null, autoSzie : String = 'left', color : uint = 0xffffff, maxWidth : Number = NaN, useHtmlMode : Boolean = false) : TextField {
			var tf : TextField;
			tf = new TextField();
			tf.mouseEnabled = tf.mouseWheelEnabled = false;
			if(txt) {
				if(useHtmlMode) {
					tf.htmlText = txt;
				} else {
					tf.text = txt;
				}
			}
			tf.textColor = color;
			tf.autoSize = autoSzie;
			if(!isNaN(maxWidth)) {
				if(tf.width > maxWidth) {
					tf.multiline = tf.wordWrap = true;
					tf.width = maxWidth;
				}
			}
			return tf;
		}

		/**
		 * 创建一个应用了样式的静态文本框(不具有交互性)
		 */
		public static function buildStyleTextField(txt : String = null, maxWidth : Number = NaN) : TextField {
			var tf : TextField;
			tf = new TextField();
			tf.mouseEnabled = tf.mouseWheelEnabled = false;
			tf.textColor = 0xffffff;
			tf.autoSize = 'left';
			if(txt)tf.text = txt;
			if(!isNaN(maxWidth)) {
				if(tf.width > maxWidth) {
					tf.multiline = tf.wordWrap = true;
					tf.width = maxWidth;
				}
			}
			return tf;
		}

		/**
		 * 删除一个显示对象容器下的所有显示对象.
		 */
		public static function removeAllChilds(container : DisplayObjectContainer) : void {
			if(container is Sprite) {
				(container as Sprite).graphics.clear();
			}
			while(container.numChildren) {
				container.removeChildAt(0);
			}
		}
		
		/**
		 * 对一个影片剪辑使用tint颜色滤镜.
		 */
		public static function applyTintColor(target : DisplayObject, tintColor : uint) : void {
			var cm : ColorMatrix = new ColorMatrix();
			cm.adjustBrightness(45);
			var filters : Array = target.filters;
			filters = [new ColorMatrixFilter(colorize(tintColor, 1)), new ColorMatrixFilter(cm)];
			target.filters = filters;
		}

		protected static var _lumR : Number = 0.212671; //Red constant - used for a few color matrix filter functions
		protected static var _lumG : Number = 0.715160; //Green constant - used for a few color matrix filter functions
		protected static var _lumB : Number = 0.072169; //Blue constant - used for a few color matrix filter functions

		private static function colorize($color : Number, $amount : Number = 1) : Array {
			if (isNaN($amount)) {
				$amount = 1;
			}
			var r : Number = (($color >> 16) & 0xff) / 255;
			var g : Number = (($color >> 8) & 0xff) / 255;
			var b : Number = ($color & 0xff) / 255;
			var inv : Number = 1 - $amount;
			var temp : Array = [inv + $amount * r * _lumR, $amount * r * _lumG,       $amount * r * _lumB,       0, 0,
							  $amount * g * _lumR,        inv + $amount * g * _lumG, $amount * g * _lumB,       0, 0,
							  $amount * b * _lumR,        $amount * b * _lumG,       inv + $amount * b * _lumB, 0, 0,
							  0, 				          0, 					     0, 					    1, 0];		
			return temp;
		}

		public static function compareFilter(filter1 : BitmapFilter, filter2 : BitmapFilter) : Boolean {
			var str1 : String = getQualifiedClassName(filter1);			var str2 : String = getQualifiedClassName(filter2);
			if(str1 != str2) {
				return false;
			} else {
				trace('to compare');
				trace(describeType(filter1));
				for each(var prop:* in filter1) {
					trace(prop);
					if(filter1[prop] != filter2[prop])return false;
				}
				return true;
			}
		}
		
		
		/**
		 * 这个url是否是图片 
		 * @param url 地址
		 * @return true:为是图片,flase为不是图片
		 * 
		 */		
		public static function isImage(url:String):Boolean{
			if(url.indexOf('jpg')!=-1||url.indexOf('bmp')!=-1||url.indexOf('png')!=-1||url.indexOf('gif')!=-1){
				return true;
			}
			return false;
		}
	}
}
